<?php


class Geo extends A_Content {
	
	private $_list_cashe = null;
	
	/**
	 * 
	 * @return SingleGeo
	 * @see A_Content::add()
	 */
	public function add() {
		$find_max_sequence = $this->_top->_sql->exe("SELECT IFNULL(MAX(`sequence`),0)+1 as s FROM `{$this->_top->_sql->tab_pref}geo`");
		$max_sequence = $find_max_sequence[0]['s'];
		 
		$this->_top->_sql->exeDummy("INSERT INTO `{$this->_top->_sql->tab_pref}geo` (`sequence`,`add_date`,`last_mod`) 
			VALUE ($max_sequence,NOW(),NOW() )");
		return $this->getById($this->_top->_sql->last_insert_id());
	}
	
	/**
	 * 
	 * @param Integer $id 
	 * @return SingleGeo 
	 * @see A_Content::getById()
	 */
	public function getById($id) {
		if (!isset($this->_cashe_obj[$id])) {
			$this->_cashe_obj[$id] = new SingleGeo($this->_top,$this,$id);
		}
		return $this->_cashe_obj[$id];
	}
	
	private function loader() {
		$this->_list_cashe = array();
		$get_list = $this->_top->_sql->exe("SELECT `geo_id` FROM `{$this->_top->_sql->tab_pref}geo` ORDER BY `sequence` ASC");
		for ($l=0;$l<count($get_list);$l++) {
			array_push($this->_list_cashe,$get_list[$l]['geo_id']);
		}
	}
	
	public function length() {
		if ($this->_list_cashe==null) {
			$this->loader();
		}
		return count($this->_list_cashe);		
	}
	/**
	 * 
	 * @param Integer $id 
	 * @return SingleGeo 
	 */
	public function item($idx) {
		if ($this->_list_cashe==null) {
			$this->loader();
		}
		return $this->getById($this->_list_cashe[$idx]);
	}
	
	
	public function rebuildContentSequence($id = null,$place = null) {
		
		
		if (($place<1 || $place>$this->length()) && $place!==null ) {
			return false;
		}
		
		
		if ($id!=null && $place != null) {
			$this->_top->_sql->exeDummy("UPDATE `{$this->_top->_sql->tab_pref}geo`
				SET `sequence`='$place' WHERE `geo_id`='{$id}'");
		}
		
		$sequence = 1;
		for ($i=0;$i<$this->length();$i++) {
			if ($this->item($i)->_content_id!=$id) {
				if ($sequence==$place && $id!=null) {
					$sequence++;
				}
				
				if (isset($this->item($i)->_content_id) && $this->item($i)->_content_id!=$id) {
					$this->_top->_sql->exeDummy("UPDATE `{$this->_top->_sql->tab_pref}geo`
						SET `sequence`='$sequence' WHERE `geo_id`='{$this->item($i)->_content_id}'");
					$sequence++;
				}
			}
		}
		
		$this->loader();
		
	}
}

class SingleGeo extends A_SingleContent {
	
	/**
	 * Название географического признака
	 *
	 * @var String
	 */
	public $value;
	
	/**
	 * Порядок в списке географических признаков
	 *
	 * @var Integer
	 */
	public $sequence;
		
	/**
	 * 
	 * @see A_SingleContent::loader()
	 */
	public function loader()
	{
		unset($this->value);
		unset($this->sequence);
		
		$get_data = $this->_top->_sql->exe("SELECT * FROM `{$this->_top->_sql->tab_pref}geo` WHERE `geo_id`={$this->_content_id}");
		if (count($get_data)==0) {
			throw $this->_top->Utils->PushError("Geo Item with Id: {$this->_content_id} NOT FOUND",'Запись не найдена');
		} else {
			$this->_data = $get_data[0];
		}
	}
	
	/**
	 * 
	 * @see A_SingleContent::__get()
	 */
	public function __get($param) {
		if (isset($this->_data[$param])) {
			return $this->_data[$param];
		} else {
			throw $this->_top->Utils->PushError("Geo Item does not have '$param' property",'Системная ошибка. Обратитесь к разработчикам');
		}
		
		
	}
	
	/**
	 * 
	 * @see A_SingleContent::__set()
	 */
	public function __set($param, $value) {
		if ($param=='sequence') {
			$this->_parent->rebuildContentSequence($this->_content_id,$value);
		} elseif ($param=='geo_id' || $param=='add_date') {
			throw $this->_top->Utils->PushError('Property "'.$param.'" unable to edit in Geo Item','Системная ошибка. Обратитесь к разработчикам' );
		} elseif (isset($this->_data[$param])) {
			$this->_data[$param] = $value;
			$ins_value = $this->_top->Utils->prepareValueForSQL($value);
			$this->_top->_sql->exeDummy("UPDATE `{$this->_top->_sql->tab_pref}geo` SET `$param`='$ins_value',last_mod=NOW() WHERE `geo_id`={$this->_content_id}");
		} else {
			throw $this->_top->Utils->PushError('Property "'.$param.'" does not exsist in Geo Item','Системная ошибка. Обратитесь к разработчикам' );
		}
		
		
		
	}
	
	/**
	 * 
	 * @see A_SingleContent::deleter()
	 */
	protected function deleter() {
		$this->_top->_sql->exeDummy("DELETE FROM `{$this->_top->_sql->tab_pref}geo` WHERE `geo_id`={$this->_content_id}");
		$this->_top->Placeholder->unlinkContentFromGeo($this);
		$this->_parent->rebuildContentSequence($this->_content_id);
	}
	

}

?>